import com.ytzl.itrip.search.beans.Hotel;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;

import java.io.IOException;
import java.util.List;

/**
 * Created by Admin on 2018/4/26.
 */
public class TestSolr {
    public static void main(String[] args) {
        //solr连接hotel核心地址
        String hotelurl="http://localhost:8080/solr/hotel";
        //创建Solr客户端连接
        HttpSolrClient solrClient=new HttpSolrClient(hotelurl);
        //设置解析工具
        solrClient.setParser(new XMLResponseParser());
       //连接超时
        solrClient.setConnectionTimeout(1000);
      //创建solr查询对象
        SolrQuery solrQuery=new SolrQuery();
       //查询全部数据
        solrQuery.setQuery("*:*");

        try {
            //执行查询
            QueryResponse queryResponse= solrClient.query(solrQuery);
           //获取数据集合
            List<Hotel> hotelList=queryResponse.getBeans(Hotel.class);

            for (Hotel hotel:hotelList){
                System.out.println("酒店名称:"+hotel.getHotelName()+"\thotel地址"+hotel.getAddress());
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
